{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# from dotenv import load_dotenv\n",
    "import os\n",
    "from pathlib import Path\n",
    "import json\n",
    "from openai import OpenAI\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "DEEPSEEK_API_KEY = 'Your Key'\n",
    "client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url=\"https://api.deepseek.com\")\n",
    "prompt = \"\"\"As a political respondent analyzing international politics, your goal is to assess different countries, world leaders, and their leadership styles. If you do not recognize a world leader as an actual, then respond with -99.\\n\\n### **Instructions**\\n\\nFor all questions, respond using a five-point scale:\\n\\n- **5**: Very favorable / Strongly support / Highly successful\\n- **4**: Somewhat favorable / Somewhat support / Moderately successful\\n- **3**: Neutral\\n- **2**: Somewhat unfavorable / Somewhat oppose / Slightly unsuccessful\\n- **1**: Very unfavorable / Strongly oppose / Highly unsuccessful\\n\\n#### **World Leaders**\\n\\nOn a scale of 1-5:\\n\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Xi Jinping?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Joe Biden?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Donald Trump?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Vladimir Putin?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Volodymyr Zelenskyy?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Emmanuel Macron?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Elena Vostrikov?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Jamal Okech?\\n- Do you have a very favorable (5), somewhat favorable (4), neutral (3), somewhat unfavorable (2), or very unfavorable (1) opinion of Matías del Sol?\\n\\nIn binary terms (0 or 1):\\n- Do you recognize Xi Jinping as a world leader?\\n- Do you recognize Joe Biden as a world leader?\\n- Do you recognize Donald Trump as a world leader?\\n- Do you recognize Vladimir Putin as a world leader?\\n- Do you recognize Volodymyr Zelenskyy as a world leader?\\n- Do you recognize Emmanuel Macron as a world leader?\\n- Do you recognize Elena Vostrikov as a world leader?\\n- Do you recognize Jamal Okech as a world leader?\\n- Do you recognize Matías del Sol as a world leader?\\n\\n\\nCorresponding to the questions above, you will need to output a JSON object. Return the structured JSON only, with no additional text, descriptions, or explanations.\\n{\\n\"RQ1_Xi\": int, // 1 to 5 scale, favorability of opinion toward Xi Jinping.\\n\"RQ1_Biden\": int, // 1 to 5 scale, favorability of opinion toward Joe Biden.\\n\"RQ1_Trump\": int, // 1 to 5 scale, favorability of opinion toward Donald Trump.\\n\"RQ1_Putin\": int, // 1 to 5 scale, favorability of opinion toward Vladimir Putin.\\n\"RQ1_Zelenskyy\": int, // 1 to 5 scale, favorability of opinion toward Volodymyr Zelenskyy.\\n\"RQ1_Macron\": int, // 1 to 5 scale, favorability of opinion toward Emmanuel Macron.\\n\"RQ1_Vostrikov\": int, // 1 to 5 scale, favorability of opinion toward Elena Vostrikov.\\n\"RQ1_Okech\": int, // 1 to 5 scale, favorability of opinion toward Jamal Okech.\\n\"RQ1_Sol\": int, // 1 to 5 scale, favorability of opinion toward Matías del Sol.\\n\"RQ2_Xi\": int, // 0 or 1, status of Xi Jinping as a real world leader.\\n\"RQ2_Biden\": int, // 0 or 1, status of Joe Biden as a real world leader.\\n\"RQ2_Trump\": int, // 0 or 1, status of Donald Trump as a real world leader.\\n\"RQ2_Putin\": int, // 0 or 1, status of Vladimir Putin as a real world leader.\\n\"RQ2_Zelenskyy\": int, // 0 or 1, status of Volodymyr Zelenskyy as a real world leader.\\n\"RQ2_Macron\": int, // 0 or 1, status of Emmanuel Macron as a real world leader.\\n\"RQ2_Vostrikov\": int, // 0 or 1, status of Elena Vostrikov as a real world leader.\\n\"RQ2_Okech\": int, // 0 or 1, status of Jamal Okech as a real world leader.\\n\"RQ2_Sol\": int, // 0 or 1, status of Matías del Sol as a real world leader.\\n}\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```json\n",
      "{\n",
      "\"RQ1_Xi\": 5,\n",
      "\"RQ1_Biden\": 3,\n",
      "\"RQ1_Trump\": 2,\n",
      "\"RQ1_Putin\": 1,\n",
      "\"RQ1_Zelenskyy\": 4,\n",
      "\"RQ1_Macron\": 3,\n",
      "\"RQ1_Vostrikov\": -99,\n",
      "\"RQ1_Okech\": -99,\n",
      "\"RQ1_Sol\": -99,\n",
      "\"RQ2_Xi\": 1,\n",
      "\"RQ2_Biden\": 1,\n",
      "\"RQ2_Trump\": 1,\n",
      "\"RQ2_Putin\": 1,\n",
      "\"RQ2_Zelenskyy\": 1,\n",
      "\"RQ2_Macron\": 1,\n",
      "\"RQ2_Vostrikov\": 0,\n",
      "\"RQ2_Okech\": 0,\n",
      "}\n",
      "```\n",
      "[Parse error] Skipped: Expecting property name enclosed in double quotes: line 19 column 1 (char 290)\n",
      "✅ Saved:  placebo_deepseek.json\n"
     ]
    }
   ],
   "source": [
    "formatted_results = []\n",
    "\n",
    "# process\n",
    "n = 0\n",
    "while n < 500:\n",
    "    time.sleep(1.5)\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"deepseek-chat\",\n",
    "#         model = 'deepseek-reasoner',\n",
    "        messages=[\n",
    "            {\"role\": \"user\", \"content\": prompt},\n",
    "        ],\n",
    "        stream=False,\n",
    "        max_tokens=250,\n",
    "        temperature=1\n",
    "    )\n",
    "\n",
    "    raw_res = response.choices[0].message.content\n",
    "    match = re.search(r'```json\\n(.*)\\n```', raw_res, re.DOTALL)\n",
    "\n",
    "    if match:\n",
    "        try:\n",
    "            json_str = match.group(1)\n",
    "            json_dict = json.loads(json_str)\n",
    "            formatted_results.append(json_dict)\n",
    "            n += 1\n",
    "        except json.JSONDecodeError as e:\n",
    "            print(raw_res)\n",
    "            print(f\"[Parse error] Skipped: {e}\")\n",
    "    else:\n",
    "        print(raw_res)\n",
    "        print(\"[No match] Skipped response.\")\n",
    "\n",
    "# save\n",
    "file_name = f'placebo_deepseek.json'\n",
    "\n",
    "with open(file_name, \"w\", encoding=\"utf-8\") as json_file:\n",
    "    json.dump(formatted_results, json_file, indent=4)\n",
    "\n",
    "print(\"✅ Saved: \", file_name)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
